package com.taxsearch.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.taxsearch.controller.room.GongAnDB;
import com.taxsearch.dao.NoCredentualsMapper;
import com.taxsearch.dao.room.RoomSelectMapper;
import com.taxsearch.util.ApiJsonRet;
import com.taxsearch.util.DbUtil;
import com.taxsearch.util.roomutil.RoomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



/**
 * FileName: NoCredentialsController.java
 *
 * @version V1.0
 * Createdate:  2018年9月6日 下午2:19:56
 * @Description: 无证审批列表
 * All rights Reserved, Designed By JS-YFB
 * Copyright:   Copyright(C) 2017-2027
 * Company      JS-YFB LTD.
 * @author: 杨陈
 */
@Controller
@RequestMapping("/HotelAndroid")
public class NoCredentialsController {
    @Autowired
    private NoCredentualsMapper noCredentualsMapper;

    @Autowired
    private RoomSelectMapper roomSelectMapper;

    /**
     * @throws SQLException
     * @throws
     * @Title: getNoCredentialsList
     * @Description: 获取无证入住列表 无证审批表中ENTERPRISE_ID 0初始待审批  1审批成功 2审批不成功  入住表中BUPLOAD 1表示待上传 3表示已上传
     * @param: @param data
     * @param: @return
     * @return: Json
     */
    @RequestMapping("/getNoCredentialsList.do")
    @ResponseBody
    public ApiJsonRet getNoCredentialsList(String data) throws SQLException {


        DbUtil.setDb("3");
        Map<String, String> map = RoomUtil.getJsonMap(data);
        String hotelId = map.get("hotelId");
        String date = map.get("date");
        String type = map.get("type");
        String zjhm = map.get("zjhm");
        String xm = map.get("xm");

        // 获取入住JDBC
        Connection gongAnDB = GongAnDB.getConnection();

        //先更新
        //1.将ENTERPRISE_ID=0 但审批成功的 变为1 并且将入住表中BUPLOAD变为1
        //查询审批成功的RECORDSNO
        List<HashMap<String, String>> listID = noCredentualsMapper.getNoCredentialsOk(hotelId);
        if (listID.size() > 0) {
            //修改无证审批表ENTERPRISE_ID 0-1
            noCredentualsMapper.updateNoCredentialsOk(hotelId, listID);
            //修改入住表BUPLOAD -》 1  JDBC
            Connection connection = DbbUtil.getConnection();

            // 修改入住状态为无证审批成功
            List<String> ids = getId(listID);
            GongAnDB.updateRuzhuStatus(gongAnDB, ids, "2");

            try {
                DbbUtil.updateCheckInBUPLOAD(connection, hotelId, listID);
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                DbbUtil.close(connection);
            }
        }

        // 获取无证审批失败的旅客
        List<String> shiBaiList = roomSelectMapper.selectWuZhengShiBai(hotelId);

        //2.将ENTERPRISE_ID=0 但审批失败的 变为2 入住表中BUPLOAD不改变
        noCredentualsMapper.updateNoCredentialsNo(hotelId);

        // 用JDBC更新入住表无证审批状态为审批失败，可能为机器审批失败，也可能为时间超过限制
        GongAnDB.updateRuzhuStatus(gongAnDB, shiBaiList, "3");
        // 关闭公安入住JDBC
        GongAnDB.close();

        //再查询
        List<HashMap<String, String>> noList = noCredentualsMapper.getNoCredentualsList(hotelId, date, type, zjhm, xm);
        ApiJsonRet json = null;
        if (noList.size() > 0) {
            json = new ApiJsonRet("200", noList);
        } else {
            json = new ApiJsonRet("400", "未查询到数据");
        }
        DbUtil.setDb("1");
        System.out.println("返回成功");
        return json;
    }

    /**
     * @Title: getNoCredentialsList
     * @Description: 获取无证入住列表 无证审批表中ENTERPRISE_ID 0初始待审批  1审批成功 2审批不成功  入住表中BUPLOAD 1表示待上传 3表示已上传
     * @param: @param data
     * @param: @return
     * @return: Json
     * @throws
     */
	/*@RequestMapping("/getNoCredentialsList.do")
	@ResponseBody
	public ApiJsonRet getNoCredentialsList(String data){
		
		DbUtil.setDb("3");
		Map<String, String> map = RoomUtil.getJsonMap(data);
		String hotelId = map.get("hotelId");
		String date = map.get("date");
		String type = map.get("type");
		String zjhm = map.get("zjhm");
		String xm = map.get("xm");
		//先更新
		//1.将ENTERPRISE_ID=0 但审批成功的 变为1 并且将入住表中BUPLOAD变为1
		//查询审批成功的RECORDSNO
		List<HashMap<String, String>> listID = noCredentualsMapper.getNoCredentialsOk(hotelId);
		if(listID.size()>0){
			//修改无证审批表ENTERPRISE_ID 0-1
			noCredentualsMapper.updateNoCredentialsOk(hotelId, listID);
			//修改入住表BUPLOAD -》 1  JDBC
			Connection connection = DbbUtil.getConnection();
			try {
				DbbUtil.updateCheckInBUPLOAD(connection, hotelId, listID);
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				DbbUtil.close();
			}
		}
		//2.将ENTERPRISE_ID=0 但审批失败的 变为2 入住表中BUPLOAD不改变
		noCredentualsMapper.updateNoCredentialsNo(hotelId);
		//再查询
		List<HashMap<String, String>> noList = noCredentualsMapper.getNoCredentualsList(hotelId, date, type, zjhm, xm);
		ApiJsonRet json = null;
		if(noList.size() >0){
			json = new ApiJsonRet("200", noList);
		}else{
			json = new ApiJsonRet("400", "未查询到数据");
		}
		DbUtil.setDb("1");
		System.out.println("返回成功");
		return json;
	}*/

    /**
     * @throws IOException
     * @throws
     * @Title: getNoCredentialsImag
     * @Description: 通过旅客ID获取审批后的图片
     * @param: @param data
     * @param: @return
     * @return: ApiJsonRet
     */
    @RequestMapping("/getNoCredentialsImag.do")
    @ResponseBody
    public ApiJsonRet getNoCredentialsImag(String data) throws IOException {
        Map<String, String> map = RoomUtil.getJsonMap(data);
        DbUtil.setDb("3");
        String ID = map.get("ID");
        List<HashMap<String, Object>> map2List = noCredentualsMapper.getNoCredentialsImag(ID);
        Connection connection = DBUtil.getConnection();
        byte[] getByteBlob2 = DBUtil.getByteBlob(connection, ID);
        ApiJsonRet json = null;
        DbUtil.setDb("1");
        if (map2List.size() <= 0) {
            json = new ApiJsonRet("400", "未查询到数据。");
            return json;
        }
        if (getByteBlob2 != null && getByteBlob2.length > 1) {
            //审批后的图片
            map2List.get(0).put("IMG", getByteBlob2);
            json = new ApiJsonRet("200", map2List.get(0));
        } else {
            //没有审批后的图片
            json = new ApiJsonRet("300", map2List.get(0));
        }
        System.out.println("返回成功");
        return json;
    }

    // 剔除多余元素,只留下id
    public List<String> getId(List<HashMap<String, String>> listID) {
        List<String> idsList = new LinkedList<>();
        for (HashMap<String, String> m : listID) {
            idsList.add(m.get("ID"));
        }
        return idsList;
    }
}











